Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

p-throttle

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-throttle

Throttle promise-returning & async functions

  • 6.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3M
decreased by-7.16%
Maintainers
1
Weekly downloads
 
Created

What is p-throttle?

The p-throttle npm package is used to throttle the execution of asynchronous functions. It allows you to limit the number of times a function can be called over a specified period, which is useful for rate-limiting API requests or controlling the flow of operations in a system.

What are p-throttle's main functionalities?

Basic Throttling

This feature allows you to throttle the execution of a function. In this example, the function can only be called twice per second.

const pThrottle = require('p-throttle');

const throttle = pThrottle({ limit: 2, interval: 1000 });

const throttled = throttle(async (index) => {
  console.log(index);
});

(async () => {
  for (let i = 0; i < 10; i++) {
    throttled(i);
  }
})();

Throttling with Promises

This feature demonstrates throttling with asynchronous functions that return promises. The function is throttled to execute once every two seconds.

const pThrottle = require('p-throttle');

const throttle = pThrottle({ limit: 1, interval: 2000 });

const throttled = throttle(async (index) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log(index);
      resolve();
    }, 1000);
  });
});

(async () => {
  for (let i = 0; i < 5; i++) {
    await throttled(i);
  }
})();

Other packages similar to p-throttle

Keywords

FAQs

Package last updated on 14 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc